package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.ShouhouOrderAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.RefundRequest.Refund_Data;
import com.banzhi.shop.entity.ShouhouOrderInfo.Orderinfo_data;
import com.banzhi.shop.model.CallBack.CancelShouhouOrderCallBack;
import com.banzhi.shop.model.CallBack.GroupRefundRequestCallBack;
import com.banzhi.shop.model.CallBack.ShouhouOrderInfoCallBack;
import com.banzhi.shop.model.ICancelShouhouOrderModel;
import com.banzhi.shop.model.IShouhouOrderInfoModel;
import com.banzhi.shop.model.Impl.CancelShouhouOrderImpl;
import com.banzhi.shop.model.Impl.CancelShouhouOrderImpl2;
import com.banzhi.shop.model.Impl.GroupRefundRequestImpl;
import com.banzhi.shop.model.Impl.ShouhouOrderInfoImpl;
import com.banzhi.shop.model.Impl.ShouhouOrderInfoImpl2;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.ListViewForScrollView;

import org.json.JSONObject;
//售后服务详情
public class ShouhouOrderInfoActivity2 extends BaseActivity implements ShouhouOrderInfoCallBack, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, CancelShouhouOrderCallBack, GroupRefundRequestCallBack {

    private ImageView ivback;
    private IShouhouOrderInfoModel model;
    /**
     * 传过来的ID
     */
    private String id;
    /**
     * 获取到的订单详情的数据实体类
     */
    private Orderinfo_data orderInfo;
    /**
     * 订单状态
     */
    private TextView tvstatus;
    /**
     * 订单金额
     */
    private TextView tv_ordermoney;
    /**
     * 快递公司
     */
    private TextView tv_kuaidigongsi;
    /**
     * 物流信息
     */
    private LinearLayout ll_wuliu;
    /**
     * 快递单号
     */
    private TextView tv_kuaidiNum;
    /**
     * 名字
     */
    private TextView tvname;
    /**
     * 电话
     */
    private TextView tvphone;
    /**
     * 地址
     */
    private TextView tvaddress;
    /**
     * 商品列表
     */
    private ListViewForScrollView listview;
    private ShouhouOrderAdapter adapter;

    /**
     * 总价
     */
    private TextView tvzongjia;
    /**
     * 快递
     */
    private TextView tvkuaidi;
    /**
     * 优惠
     */
    private TextView tvyouhui;
    /**
     * 订单总价
     */
    private TextView tvdingdanzongjia;
    /**
     * 实际付款
     */
    private TextView tvshijifukuan;
    /**
     * 订单号
     */
    private TextView tvdingdanhao;
    /**
     * 订单创建时间
     */
    private TextView tvtime;
    /**
     * 复制订单号
     */
    private TextView tvfuzhi;
    /**
     * 申请售后
     */
    private TextView tv_shenqingshouhou;
    /**
     * 取消订单
     */
    private TextView tv_cancelorder;

    /**
     * 取消售后申请
     */
    private ICancelShouhouOrderModel cancelShouhouOrderModel;
    private TextView tvzhifutime;
    private TextView tvzhifutype;
    private TextView tvdanhao;
    private TextView tvfuzhi2;
    /**
     * 传过来的字段 用来判断是自己的订单还是下线的订单
     */
    private String ShoperOrder;
    private RelativeLayout llbom;
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView tvdianzhulijian;


    private TextView tvShouhouMoney;
    private TextView mTvApply;
    private GroupRefundRequestImpl mGroupRefundRequestModel;
    private String mOid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouhou_order_info);
        Translucent.setTranslucent(this);
        initViews();
        setListeners();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        mOid = intent.getStringExtra("oid");
        ShoperOrder = intent.getStringExtra("shoperOrder");
        model = new ShouhouOrderInfoImpl2();
        cancelShouhouOrderModel = new CancelShouhouOrderImpl2();
        if (ASUtils.isCanUse(ShoperOrder) && !"1".equals(ShoperOrder)) {
            llbom.setVisibility(View.GONE);
        }
        mGroupRefundRequestModel = new GroupRefundRequestImpl();
    }

    @Override
    protected void onStart() {
        super.onStart();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(id, this);

    }

    private void initViews() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvstatus = (TextView) findViewById(R.id.tv_stat);
        tv_ordermoney = (TextView) findViewById(R.id.tv_ordermoney);
        tv_kuaidigongsi = (TextView) findViewById(R.id.tv_kuaidigongsi);
        tv_kuaidiNum = (TextView) findViewById(R.id.tv_kuaidiNum);
        ll_wuliu = (LinearLayout) findViewById(R.id.ll_wuliu);
        tvname = (TextView) findViewById(R.id.tv_name);
        tvphone = (TextView) findViewById(R.id.tv_phone);
        tvaddress = (TextView) findViewById(R.id.tv_address);
        listview = (ListViewForScrollView) findViewById(R.id.listview);
        tvdianzhulijian = (TextView) findViewById(R.id.tv_dianzhulijian);
        tvzongjia = (TextView) findViewById(R.id.tv_zongjia);
        tvkuaidi = (TextView) findViewById(R.id.tv_kuaidi);
        tvyouhui = (TextView) findViewById(R.id.tv_youhui);
        tvdingdanzongjia = (TextView) findViewById(R.id.tv_dingdanzongjia);
        tvshijifukuan = (TextView) findViewById(R.id.tv_shifukuan);
        tvdingdanhao = (TextView) findViewById(R.id.tv_bianhao);
        tvfuzhi = (TextView) findViewById(R.id.tv_fuzhi);
        tvtime = (TextView) findViewById(R.id.tv_time);
        tv_shenqingshouhou = (TextView) findViewById(R.id.tv_shenqingshouhou);
        tv_cancelorder = (TextView) findViewById(R.id.tv_cancelorder);
        tvzhifutime = (TextView) findViewById(R.id.tv_zhifutime);
        tvzhifutype = (TextView) findViewById(R.id.tv_zhifu);
        tvdanhao = (TextView) findViewById(R.id.tv_danhao);
        tvfuzhi2 = (TextView) findViewById(R.id.tv_fuzhi2);
        mTvApply = (TextView) findViewById(R.id.tv_apply);
        llbom = (RelativeLayout) findViewById(R.id.ll_bom);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        tvShouhouMoney = (TextView) findViewById(R.id.tv_souhoumoney);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvfuzhi.setOnClickListener(this);
        tv_shenqingshouhou.setOnClickListener(this);
        tv_cancelorder.setOnClickListener(this);
        tvfuzhi2.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        mTvApply.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_fuzhi:
                ShareUtils.copyLianjie(this, tvdingdanhao.getText().toString().trim());
                break;
            case R.id.tv_shenqingshouhou:
                Intent intent = new Intent(this, ShouhouShenqingActivity2.class);
                if (ASUtils.isCanUse(orderInfo.getRefundid())) {
                    intent.putExtra("refundid", orderInfo.getRefundid());
                    intent.putExtra("id", orderInfo.getOrderid());
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.tv_cancelorder:
                showPopupwindow(id);
                break;
            case R.id.tv_apply:
                mGroupRefundRequestModel.getData(mOid,this);
                break;

            case R.id.tv_fuzhi2:
                ShareUtils.copyLianjie(this, tvdanhao.getText().toString().trim());
                break;
        }
    }

    /**
     * 获取订单详情数据成功
     *
     * @param object
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        orderInfo = (Orderinfo_data) object;
        setViews();
    }

    private void setViews() {
        tvdanhao.setText(orderInfo.getRefundno());
        if ("-2".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("已取消");
            tv_cancelorder.setVisibility(View.GONE);
        } else if ("-1".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("已拒绝");
            tv_cancelorder.setVisibility(View.GONE);
            mTvApply.setVisibility(View.VISIBLE);
        } else if ("0".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("申请中");
            tv_cancelorder.setVisibility(View.VISIBLE);
        } else if ("1".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("已完成");
            tv_cancelorder.setVisibility(View.GONE);
        } else if ("3".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("处理中");
            tv_cancelorder.setVisibility(View.VISIBLE);
        } else if ("4".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("处理中");
            tv_cancelorder.setVisibility(View.GONE);
        } else if ("5".equals(orderInfo.getRefundstatus())) {
            tvstatus.setText("处理中");
            tv_cancelorder.setVisibility(View.GONE);
        }
//
//        if ("1".equals(orderInfo.getStatus())) {
////            tvzhuangtai.setText("待发货");
//            ll_wuliu.setVisibility(View.GONE);
//        } else if ("3".equals(orderInfo.getStatus())) {
////            tvzhuangtai.setText("已完成");
//            tv_kuaidigongsi.setText(orderInfo.getExpresscom());
//            tv_kuaidiNum.setText(orderInfo.getExpresssn());
//        }
        if (ASUtils.isCanUse(orderInfo.getGoodsprice())) {
            tv_ordermoney.setText(NumberUtils.DecimalFormat(orderInfo.getGoodsprice()));
        }
        tvname.setText(orderInfo.getRealname());
        tvphone.setText(orderInfo.getMobile());
        tvaddress.setText(orderInfo.getAddressDetial());
        if (adapter == null) {
            adapter = new ShouhouOrderAdapter(this, orderInfo.getOrderGoods());
            listview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (ASUtils.isCanUse(orderInfo.getGoodsprice())) {
            tvzongjia.setText(NumberUtils.DecimalFormat(orderInfo.getGoodsprice()));
        }
        if (ASUtils.isCanUse(orderInfo.getDispatchprice())) {
            tvkuaidi.setText(NumberUtils.DecimalFormat(orderInfo.getDispatchprice()));
        }
        if (ASUtils.isCanUse(orderInfo.getDiscountprice())) {
            tvyouhui.setText(NumberUtils.DecimalFormat(orderInfo.getDiscountprice()));
        }
        if (ASUtils.isCanUse(orderInfo.getPrice())) {
            tvdingdanzongjia.setText(NumberUtils.DecimalFormat(orderInfo.getPrice()));
        }
        if (ASUtils.isCanUse(orderInfo.getPrice())) {
            tvshijifukuan.setText(NumberUtils.DecimalFormat(orderInfo.getPrice()));
        }
        if (ASUtils.isCanUse(orderInfo.getCommission())) {
            tvdianzhulijian.setText(NumberUtils.DecimalFormat(orderInfo.getCommission()));
        }
        tvdingdanhao.setText(orderInfo.getOrderno());
        if (ASUtils.isCanUse(orderInfo.getCreatetime())) {
            tvtime.setText(DateUtil.getData(orderInfo.getCreatetime()));
        }
        if (ASUtils.isCanUse(orderInfo.getPaytime())) {
            tvzhifutime.setText(DateUtil.getData(orderInfo.getPaytime()));
        }
        tvzhifutype.setText(orderInfo.getPay_type());
        if (ASUtils.isCanUse(orderInfo.getApplyprice())) {
            tvShouhouMoney.setText(NumberUtils.DecimalFormat(orderInfo.getApplyprice()));
        }
    }

    /**
     * 获取订单详情数据失败
     *
     * @param object
     */
    @Override
    public void ongetDataError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 取消申请成功
     *
     * @param object
     */
    @Override
    public void CancelOrderSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        ToastUtils.makeToastShort("取消售后成功");
        finish();
    }

    /**
     * 取消申请失败
     *
     * @param object
     */
    @Override
    public void CancelOrderError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 点击取消订单
     *
     * @param id
     */
    private void showPopupwindow(final String id) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_cancel_shouhou, null);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadingDialogUtils.showProgress(ShouhouOrderInfoActivity2.this, "正在加载,请稍后~");
                cancelShouhouOrderModel.CancelOrder(id, ShouhouOrderInfoActivity2.this);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(id, this);
    }

    @Override
    public void onGroupRefundRequestSuccess(Object object) {
        Refund_Data refund = (Refund_Data) object;
        Intent intent2 = new Intent(ShouhouOrderInfoActivity2.this, RefundRequestActivity2.class);
        intent2.putExtra("data", refund);
        intent2.putExtra("Orderid",mOid);
        intent2.putExtra("status","1");
        startActivity(intent2);
    }

    @Override
    public void onGroupRefundRequestError(Object object) {

    }
}
